package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.DataCenterService;
import com.yixun.qfbao.apidto.GetDevPremisesExportReqDto;
import com.yixun.qfbao.apidto.GetDevPremisesExportResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.DevPremisesExport;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.CalculateUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetDevPremisesExportReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分页获取推荐人数据报表
 *
 * @classname: GetReportRecommendHander.java
 * @author: Beau
 * @create: 2019/12/17 14:56
 * @version: 1.0.0
 **/
@Service("getReportRecommendHander")
public class GetReportRecommendHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetReportRecommendHander.class);
    @Autowired
    private DataCenterService dataCenterService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetDevPremisesExportReqVo reqDto1 = (GetDevPremisesExportReqVo) reqDto;
        GetDevPremisesExportReqDto dto = reqDto1.getParams();
        BaseResponseDTO<GetDevPremisesExportResDto> res = new BaseResponseDTO<>();
        GetDevPremisesExportResDto resDto = null;
        PartnerInfo pi = loginUtils.getUser(dto.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = format.format(new Date());

            dto.setDevCompanyId(pi.getHigherid());
            resDto = dataCenterService.getDevPremises(dto);
            List<DevPremisesExport> exports = resDto.getDevPremisesExportPage().getRows();
            List<DevPremisesExport> collect = exports.stream().filter(devPremisesReport -> {
                String signRate = CalculateUtil.signRate(devPremisesReport.getCustomerSuccessCount() + "", devPremisesReport.getSignCount() + "");
                devPremisesReport.setSignRate(signRate);
                return true;
            }).collect(Collectors.toList());

            resDto.getDevPremisesExportPage().setRows(collect);
            resDto.setExpirationDate(nowDate);
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
            return res;
        } catch (Exception e) {
            logger.error("GetDevPremisesHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("查找数据报表失败");
            return res;
        }
    }
}
